import { baseRequest } from '@/common/axios'
import appConfig from '@/static/app.config.js'
import axios from 'axios'

function objectToUrlParams(obj) {
	let str = ''
	for (let key in obj) {
		if (str != '') {
			str += '&'
		}
		str += key + '=' + obj[key]
	}
	return str
}

// 生成9位随机数字
function generateRandomNumber() {
	let randomNumber = ''
	for (let i = 0; i < 9; i++) {
		randomNumber += Math.floor(Math.random() * 10) // 生成0到9之间的随机整数
	}
	return randomNumber
}
export default {
	// 获取随机code和realKey
	getCodeKey(data) {
		return baseRequest('/api/wpd-sanming/captcha/image/' + generateRandomNumber(), data, 'get')
	},
	// 获取token
	getToken(data) {
		// return baseRequest(appConfig['wpd-sanming'] + '/wpd-sanming/oauth/token', data, 'get')
		// const queryString = '?' + objectToUrlParams(data)
		let url = '/api/wpd-sanming/oauth/token'
		return axios({
			method: 'get',
			url: url,
			params: data,
			auth: {
				username: 'wpdapp',
				password: 'wpdapp'
			}
			// headers: {
			// 	Authorization: 'Basic d3BkOndwZA=='
			// }
		})
	},
	// 验证token
	checkToken(data) {
		// return baseRequest(appConfig['wpd-sanming'] + '/wpd-sanming/oauth/check_token', data, 'post')
		return axios({
			method: 'post',
			url: '/api/wpd-sanming/oauth/check_token',
			params: {
				token: data
			},
			auth: {
				username: 'wpdapp',
				password: 'wpdapp'
			}
			// headers: {
			// 	Authorization: 'Basic d3BkOndwZA=='
			// }
		})
	},
	// 获取用户信息
	getUserInfo(data) {
		// return baseRequest(appConfig['wpd-sanming'] + '/wpd-sanming/system/user/info', data, 'get')
		return axios({
			method: 'get',
			url: '/api/wpd-sanming/system/user/info',
			params: {},
			headers: {
				Authorization: 'Bearer ' + data.Authorization,
				token: data.Authorization
			}
		})
	},
	// 获取随机code和realKey
	getDeptTree(data) {
		// return baseRequest(appConfig['wpd-sanming'] + '/wpd-sanming/system/dept/deptTree', data, 'get')
		return axios({
			method: 'get',
			url: '/api/wpd-sanming/system/dept/deptTree',
			params: {},
			headers: {
				Authorization: 'Bearer ' + data.Authorization,
				token: data.Authorization
			}
		})
	},
	// 获取随机code和realKey
	getImage(data) {
		// return baseRequest(appConfig['wpd-sanming'] + '/wpd-sanming/system/dept/deptTree', data, 'get')
		return axios({
			method: 'get',
			url: '/api/wpd-sanming/captcha/image/' + generateRandomNumber(),
			params: {}
		})
	},
	// 添加uuid设备id用户id推送
	addOrEdit(data) {
		return axios({
			method: 'post',
			url: '/api/smsl/biz/warningPushUser/addOrEdit',
			data: data
		})
		// return baseRequest('/api/smsl/biz/warningPushUser/addOrEdit', data, 'post')
	}
}
